<template>
	<view class="container">
		<view class="list-cell b-b m-t" @click="navTo('/pages/core/user/profile')" hover-class="cell-hover" :hover-stay-time="50">
			<text class="cell-tit">个人资料</text>
			<text class="cell-more xyicon xyicon-right"></text>
		</view>
		<view class="list-cell b-b" @click="navTo('/pages/user_address/address/my')" hover-class="cell-hover" :hover-stay-time="50">
			<text class="cell-tit">收货地址</text>
			<text class="cell-more xyicon xyicon-right"></text>
		</view>
		<view class="list-cell hidden" @click="navTo('/pages/user_cert/cert/my')" hover-class="cell-hover" :hover-stay-time="50">
			<text class="cell-tit">实名认证</text>
			<text class="cell-more xyicon xyicon-right"></text>
		</view>
		<view class="list-cell m-t hidden">
			<text class="cell-tit">消息推送</text>
			<switch checked color="#fa436a" @change="switchChange" />
		</view>
		<view class="list-cell m-t b-b" @click="clearRuntime" hover-class="cell-hover" :hover-stay-time="50">
			<text class="cell-tit">清除缓存</text>
			<text class="cell-more xyicon xyicon-right"></text>
		</view>
		<view class="list-cell" @click="checkUpdate">
			<text class="cell-tit">当前版本</text>
			<!-- #ifdef APP-PLUS -->
			<text class="cell-tip">{{app.version}} 检查更新</text>
			<!-- #endif -->
			<!-- #ifndef APP-PLUS -->
			<text class="cell-tip">{{app.version}}</text>
			<!-- #endif -->
			<text class="cell-more xyicon xyicon-right"></text>
		</view>
		<view class="list-cell b-b" @click="navTo('/pages/core/settings/about')" hover-class="cell-hover" :hover-stay-time="50">
			<text class="cell-tit">关于</text>
			<text class="cell-more xyicon xyicon-right"></text>
		</view>
		<view class="list-cell log-out-btn" @click="logout">
			<text class="cell-tit">退出登录</text>
		</view>
	</view>
</template>

<script>
	import {mapState, mapMutations} from 'vuex';
	export default {
		data() {
			return {};
		},
		computed: {
			...mapState(['app', 'user'])
		},
		onLoad() {
		},
		methods:{
			navTo(url){
				uni.navigateTo({
					url: url
				})
			},
			clearRuntime () {
				this.util.showToast({
					title: '清除成功',
					icon: 'success',
					duration: 1000
				});
			},
			checkUpdate(){
				let _this = this;
				// #ifdef APP-PLUS
				// 锁定屏幕方向
				plus.screen.lockOrientation('portrait-primary'); //锁定
				// 热更新
				plus.runtime.getProperty(plus.runtime.appid, async function(widgetInfo) {
					let res = await _this.util.request({
						url: '/v1/cloud/index/appUpdate',
						data: {
							version: widgetInfo.version,
							name: widgetInfo.name
						}
					});
					if (res.code == 200) {
						if (res.data.versionInfo.wgtUrl) {
							uni.downloadFile({
								url: res.data.versionInfo.wgtUrl,
								success: (downloadResult) => {
									if (downloadResult.statusCode === 200) {
										plus.runtime.install(downloadResult.tempFilePath, {
											force: false
										}, function() {
											console.log('install success...')
											plus.runtime.restart()
										}, function(e) {
											console.error('install fail...')
										})
									}
								}
							});
						} else {
							// 提醒用户更新
							uni.showModal({
								title: '更新提示',
								content: '是否选择更新',
								success: (ret) => {
									if (ret.confirm) {
										plus.runtime.openURL(res.data.versionInfo.appDownurl);
									}
								}
							})
						}
					} else {
						_this.util.showToast({
							title: res.msg,
							icon: 'success',
							duration: 1000
						});
					}
				});
				// #endif
			},
			//退出登录
			logout(){
				uni.showModal({
				    content: '确定要退出登录么',
				    success: (e)=>{
				    	if(e.confirm){
				    		setTimeout(async ()=>{
								// 先清除本地token防止接口挂了无法注销
								this.$store.commit('user/setToken', '');
								this.$store.commit('user/setUserInfo', '');
								// 清除服务器缓存
				    			let res = await this.util.request({
				    				url: '/v1/core/user/logout',
				    				method: 'delete',
				    			});
				    			if (res.code == 200) {
				    				this.util.showToast({
				    					title: res.msg,
				    					icon: 'success',
				    					duration: 1000
				    				});
				    				this.util.switchTab({
				    					url: '/pages/home'
				    				})
				    			} else {
				    				this.util.showToast({
				    					title: res.msg,
				    					icon: 'loading',
				    					duration: 2000
				    				});
				    			}
				    		}, 200)
				    	}
				    }
				});
			},
			//switch
			switchChange(e){
				let statusTip = e.detail.value ? '打开': '关闭';
			},

		}
	}
</script>

<style lang='scss'>
	page {
		background: $page-color-base;
	}
	.list-cell {
		display:flex;
		align-items:baseline;
		padding: 20upx $page-row-spacing;
		line-height:60upx;
		position:relative;
		background: #fff;
		justify-content: center;
		&.log-out-btn {
			margin-top: 40upx;
			.cell-tit{
				color: $uni-color-primary;
				text-align: center;
				margin-right: 0;
			}
		}
		&.cell-hover {
			background:#fafafa;
		}
		&.b-b:after {
			left: 30upx;
		}
		&.m-t {
			margin-top: 16upx; 
		}
		.cell-more {
			align-self: baseline;
			font-size:$font-lg;
			color:$font-color-light;
			margin-left:10upx;
		}
		.cell-tit {
			flex: 1;
			font-size: $font-base + 2upx;
			color: $font-color-dark;
			margin-right:10upx;
		}
		.cell-tip {
			font-size: $font-base;
			color: $font-color-light;
		}
		switch {
			transform: translateX(16upx) scale(.84);
		}
	}
</style>
